package View;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import Model.Computation;

public class NewtonGraph extends JPanel{
	
	private XYDataset newtonDataSet;
	private JFreeChart newtonLineChart;
	private ChartPanel newtonChartPanel;
	
	public NewtonGraph(){
		setLayout(new MigLayout());
		newtonDataSet = createDataset();
		newtonLineChart = createChart(newtonDataSet);
		newtonChartPanel = new ChartPanel(newtonLineChart);
		add(newtonChartPanel,BorderLayout.CENTER);
	}
		
	private XYDataset createDataset(){
		final XYSeriesCollection dataset = new XYSeriesCollection();
		return dataset;	
	}
	 
	 private XYDataset createXYDataset(Computation newComputation){
		 
		  final XYSeries polynomial = new XYSeries("Polynomial");  
		  for( int i = 0; i < newComputation.getX().size(); i++)
			  polynomial.add(newComputation.getX().get(i), newComputation.getY().get(i));
	
		  final XYSeriesCollection dataset = new XYSeriesCollection();
		  dataset.addSeries(polynomial);

		  return dataset;
	 }
	 
	 private XYDataset createDataset(Computation newComputation, int counter){
		 
		  final XYSeries polynomial = new XYSeries("Polynomial");  
		  for( int i = 0; i < newComputation.getX().size(); i++)
			  polynomial.add(newComputation.getX().get(i), newComputation.getY().get(i));
		 
		  final XYSeries newton = new XYSeries("Newton");
		  newton.add(newComputation.getFirstColumn().get(counter), newComputation.getSecondColumn().get(counter));
		  
		  final XYSeriesCollection dataset = new XYSeriesCollection();
		  dataset.addSeries(polynomial);
		  dataset.addSeries(newton);
		  
		  return dataset;
		 
	 }
	 
	 private JFreeChart createChart(XYDataset dataset) {
		
			JFreeChart chart = ChartFactory.createXYLineChart( "Newton Method", "X", "Y", dataset, PlotOrientation.VERTICAL, true, true, false );
			chart.setBackgroundPaint(Color.white);
			XYPlot plot = chart.getXYPlot();
			plot.setBackgroundPaint(Color.lightGray);
			plot.setDomainGridlinePaint(Color.white);
			plot.setRangeGridlinePaint(Color.white);
			
			final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
		    renderer.setSeriesLinesVisible(1, false);
		    renderer.setSeriesShapesVisible(0, false);
		    plot.setRenderer(renderer);
			
			NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
			rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
			return chart;
		}
	 
	
	public void updateValues(Computation computation){
		newtonDataSet = createXYDataset(computation);
		newtonLineChart = createChart(newtonDataSet);
		newtonChartPanel.setChart(newtonLineChart);
	}
	
	public void simulatePlot(Computation computation, int counter){
		newtonDataSet = createDataset(computation, counter);
		newtonLineChart = createChart(newtonDataSet);
		newtonChartPanel.setChart(newtonLineChart);
	}
}
